/*
 * ******************************************************************************
 *   Copyright (c) 2013-2014 Gabriele Mariotti.
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 *  *****************************************************************************
 */

package it.gmariotti.cardslib.demo.fragment.nativeview;

import it.gmariotti.cardslib.demo.ResourceTable;
import it.gmariotti.cardslib.demo.cards.MayKnowCard;
import it.gmariotti.cardslib.demo.cards.SuggestedCard;
import it.gmariotti.cardslib.demo.drawable.CircleDrawable;
import it.gmariotti.cardslib.demo.fragment.v1.MaterialV1Fragment;
import it.gmariotti.cardslib.library.Constants;
import it.gmariotti.cardslib.library.internal.Card;
import it.gmariotti.cardslib.library.internal.CardThumbnail;
import it.gmariotti.cardslib.library.view.CardView;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.ScrollView;
import ohos.app.Context;
import ohos.hiviewdfx.HiLog;
import ohos.media.image.PixelMap;

/**
 * Card Examples
 *
 * @author Gabriele Mariotti (gabri.mariotti@gmail.com)
 */
public class NativeMiscCardFragment extends MaterialV1Fragment {

    protected ScrollView mScrollView;
    float mElevationCircle;
    int mSizeCircle;


    @Override
    protected int getSubTitleHeaderResourceId() {
        return ResourceTable.String_header_title_subtitle_ex_misc;
    }

    @Override
    protected int getTitleHeaderResourceId() {
        return ResourceTable.String_header_title_group2;
    }

    @Override
    protected String getDocUrl() {
        return "";
    }

    @Override
    protected String getSourceUrl() {
        return "";
    }

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        setUIContent(ResourceTable.Layout_demo_fragment_native_misc_card);
        mScrollView = (ScrollView) this.findComponentById(ResourceTable.Id_card_scrollview);
        try {
            mElevationCircle = getResourceManager().getElement(ResourceTable.Float_circle_elevation).getFloat();
            mSizeCircle = getResourceManager().getElement(ResourceTable.Float_circle_size).getInteger();
        } catch (Exception e) {
            HiLog.error(Constants.LABEL_LOG, e.toString());
        }

        initCard();
    }

    private void initCard() {

        initCardMayKnow();
        initCardSuggested();
        initCircleCard();

    }


    /**
     * This method builds a simple card
     */
    private void initCardMayKnow() {

        //Create a Card
        MayKnowCard card = new MayKnowCard(this);
        card.setShadow(false);

        //Set card in the cardView
        CardView cardView = (CardView) this.findComponentById(ResourceTable.Id_carddemo_MayKnow);
        cardView.setCard(card);

        MayKnowCard card2 = new MayKnowCard(this);
        card2.addCardHeader(null);
        card2.setShadow(true);
        CardView mayView2 = (CardView) this.findComponentById(ResourceTable.Id_carddemo_MayKnow2);
        mayView2.setCard(card2);

    }

    /**
     * This method builds a suggested card example
     */
    private void initCardSuggested() {

        SuggestedCard card = new SuggestedCard(this);
        CardView cardView = (CardView) this.findComponentById(ResourceTable.Id_carddemo_suggested);
        cardView.setCard(card);
    }


    private void initCircleCard() {

        Card card = new Card(this);
        card.setTitle("Title");
        //card.setBackgroundResourceId(ResourceTable.Color_demo_card_background_color1);
        CardThumbnailCircle thumb = new CardThumbnailCircle(this);
        card.addCardThumbnail(thumb);

        CardView cardView = (CardView) this.findComponentById(ResourceTable.Id_carddemo_circleleft);
        cardView.setCard(card);

    }

    public class CardThumbnailCircle extends CardThumbnail {

        public CardThumbnailCircle(Context context) {
            super(context);

            float density = getContext().getResourceManager().getDeviceCapability().screenDensity;
            int size = (int) (70 * density);
            setUrlResource("https://lh5.googleusercontent.com/-squZd7FxR8Q/UyN5UrsfkqI/AAAAAAAAbAo/VoDHSYAhC_E/s" + size + "/new%2520profile%2520%25282%2529.jpg");
            setErrorResource(ResourceTable.Media_ic_ic_error_loading);
        }

        @Override
        public boolean applyBitmap(final Component imageView, PixelMap bitmap) {
            CircleDrawable circle = new CircleDrawable(bitmap);
            imageView.setBackground(circle);
            return true;
        }
    }

}
